//
//  SJOrderToast.swift
//  Order
//
//  Created by Saxon on 2018/3/29.
//  Copyright © 2018年 remotevillage. All rights reserved.
//

import UIKit

enum SJToastType {
    case normal
    case success
    case fail
}

class SJToast: UIView {
    var style:SJToastType = .normal
    fileprivate static let shared = SJToast()
    fileprivate var button:UIButton = UIButton()
    static func show(_ text:String) {
        SJToast.shared.hide()
        SJToast.shared.show(.normal, text: text)
        
    }
    static func successShow(_ text:String) {
        SJToast.shared.hide()
        SJToast.shared.show(.success, text: text)
        
    }
    static func failShow(_ text:String) {
        SJToast.shared.hide()
        SJToast.shared.show(.fail, text: text)
        
    }
    init() {
        super.init(frame: CGRect(x: 10, y: UIScreen.height, width: UIScreen.width-20, height:52))
    }
    func show(_ style: SJToastType = SJToastType.normal, text:String) {
        
        
        UIApplication.shared.keyWindow?.addSubview(self)
        
        layer.cornerRadius = 2;
        clipsToBounds = true
        
        button.frame = self.bounds
        addSubview(button)
        button.isEnabled = false
        button.setTitle(text, for: .disabled)
        button.setTitleColor(UIColor.white, for: .disabled)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        switch style {
        case .normal:
            backgroundColor = UIColor.hex("#323C49", alpha: 0.8)
            button.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0)
            button.setImage(UIImage(named:""), for: .disabled)
            break;
        case .success:
            backgroundColor = UIColor.hex("#80CB9A", alpha: 0.9)
            button.setImage(UIImage(named:"toast_success"), for: .disabled)
            button.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0)
            break;
        case .fail:
            backgroundColor = UIColor.hex("#FB5F5F", alpha: 0.9)
            button.setImage(UIImage(named:"toast_fail"), for: .disabled)
            button.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0)
            break;
        }
        
        UIView.animate(withDuration: 0.2, animations: {
            self.mj_y = UIScreen.height-62
        }) { (falg) in
            let t = DispatchTime.now() + 2
            DispatchQueue.main.asyncAfter(deadline: t) {
                self.hide()
            }
        }
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(touchBtn))
        self.addGestureRecognizer(tap)
    }
    func touchBtn() {
        self.mj_y = UIScreen.height
    }
    fileprivate func hide() {
        UIView.animate(withDuration: 0.2, animations: {
            self.mj_y = UIScreen.height
        })
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        fatalError("Please use init(frame: CGRect, style: SJDatePickerStyle)")
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
